<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\admin\controller;

use think\Controller;
use app\admin\controller\Admin;
use think\Db;
use utils\Tools\Express as MyExpress;

class Express extends Admin {

    /**
     * @title 快递
     */
    public function index() {


        $lists = db('express')->order('listorder asc')->select();


        $this->assign('lists', $lists);

        builder('list')
                ->addItem('id', '#')
                ->addSortItem('listorder', '排序', 'express')
                ->addItem('title', '公司名称')
                ->addItem('alias', '公司代码')
                ->addAction('编辑', 'edit', '<i class="layui-icon layui-icon-edit"></i>', 'layui-btn-normal openbox')
                ->addAction('删除', 'delete', '<i class="layui-icon layui-icon-delete"></i>', 'ajax-get confirm layui-btn-danger')
                ->build();


        return view();
    }

    /**
     * @title 添加一个前台会员
     */
    public function add() {


        if (request()->isPost()) {

            $post = request()->post();

            $data['title'] = $post['title'] ?? '';
            $data['alias'] = $post['alias'] ?? '';

            $insert_id = db('express')->insertGetId($data);

            if ($insert_id !== FALSE) {

                // 
                db('express')->where('id', $insert_id)->setField('listorder', $insert_id);

                $this->success('', url('index'));
            } else {
                $this->error('添加失败');
            }
        } else {
            builder('form')
                    ->addItem('title', 'input', '公司名称<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('alias', 'input', '公司代码<font color="red">*</font>', '', 'lay-verify="required"')
                    ->build();
            return view();
        }
    }

    /**
     * @title 导航编辑
     */
    public function edit($id) {

        empty($id) && $this->error('参数不能为空');

        if (request()->isPost()) {



            $post = request()->post();

            $data['title'] = $post['title'] ?? '';
            $data['alias'] = $post['alias'] ?? '';



            if (db('express')->where('id', $id)->update($data) !== FALSE) {
                $this->success('', url('index'));
            } else {
                $this->error('添加失败');
            }
        } else {

            $one = db('express')->where('id', $id)->find();


            builder('form')
                    ->addItem('title', 'input', '公司名称<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('alias', 'input', '公司代码<font color="red">*</font>', '', 'lay-verify="required"')
                    ->build($one);
            return view();
        }
    }

    /**
     * @title 快递删除
     * @param type $id
     */
    public function delete($id) {

        empty($id) && $this->error('参数不能为空');

        if (db('express')->where('id', $id)->delete() !== FALSE) {
            $this->success('', url('index'));
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * @title 支持的快递公司
     */
    public function express_info() {
        
        //
        $key = config('express.key');
        $express = new MyExpress($key); //初始化类


        $express_lists = $express->getComs();


        if (isset($express_lists['resultcode']) && $express_lists['resultcode'] == 200) {
            $this->assign('lists', $express_lists['result']);
            return view();
        } else {
            $this->error('查询快递公司失败');
        }
    }

}
